$(document).ready(function () {
	$("input[name=code]").on('focus',function(e){
	    reloadCode();
	});

    $('input').iCheck({
        checkboxClass: 'icheckbox_minimal-tpv',
        radioClass: 'iradio_minimal-tpv',
        increaseArea: '20%'
    });

    var $formPanelTwo = $('.form-panel.two');
    if ($formPanelTwo.length) {
        var panelOne = $formPanelTwo.height();
        var panelTwo = $formPanelTwo[0].scrollHeight;

        $formPanelTwo.not('.form-panel.two.active').on('click', function (e) {
            e.preventDefault();

            $('.form-toggle').addClass('visible');
            $('.form-panel.one').addClass('hidden');
            $('.form-panel.two').addClass('active');
            $('.form').animate({
                'height': panelTwo
            }, 200);
        });
    }

    $('.form-toggle').on('click', function (e) {
        e.preventDefault();
        $(this).removeClass('visible');
        $('.form-panel.one').removeClass('hidden');
        $('.form-panel.two').removeClass('active');
        $('.form').animate({
            'height': panelOne + 92
        }, 200);
    });
    //对input进行keydown后做处理 modify by aaron 20181227
    $('.tpv-login-content input').on('keydown', function (e) {
        if (window.event)
            e = window.event;
        var code = e.charCode || e.keyCode;
        if (code === 13) {
            login();
        }
    });

});


function reloadCode() {
    $("#validateCodeImg").attr("src", ctx + "gifCode?data=" + new Date().getTime() + "");
}

function login() {
    var $loginButton = $("#loginButton");
    var username = $(".one input[name='username']").val().trim();
    var password = $(".one input[name='password']").val().trim();
    var code = $(".one input[name='code']").val().trim();
    var rememberMe = $(".one input[name='rememberme']").is(':checked');

    /**
     * by aaron.wei
     */
    var  _msg1 = $MB.i18n('login.user.msg'),
         _msg2 = $MB.i18n('login.password.msg'),
         _msg3 = $MB.i18n('login.code.msg'),
         _login_txt = $MB.i18n('login.btn');

    if (username === "") {
        $MB.n_warning(_msg1);
        return;
    }
    if (password === "") {
        $MB.n_warning(_msg2);
        return;
    }
    if (code === "") {
        $MB.n_warning(_msg3);
        return;
    }
    $loginButton.html("").append("<div class='login-loder'><div class='line-scale'><div></div><div></div><div></div><div></div><div></div></div></div>");

    $.ajax({
        type: "post",
        url: ctx + "login",
        data: {
            "username": username,
            "password": password,
            "code": code,
            "rememberMe": rememberMe
        },
        dataType: "json",
        success: function (r) {
            if (r.code === 0) {
                location.href = ctx + 'index';
            } else if (r.code === -1) {
                $MB.n_warning(r.msg, function () {
                    location.href = ctx + 'index';
                });
            } else {
                reloadCode();
                $MB.n_warning(r.msg);
                $loginButton.html(_login_txt);
            }
        }
    });
}

function regist() {
    var username = $(".two input[name='username']").val().trim();
    var password = $(".two input[name='password']").val().trim();
    var cpassword = $(".two input[name='cpassword']").val().trim();
    if (username === "") {
        $MB.n_warning("用户名不能为空！");
        return;
    } else if (username.length > 10) {
        $MB.n_warning("用户名长度不能超过10个字符！");
        return;
    } else if (username.length < 3) {
        $MB.n_warning("用户名长度不能少于3个字符！");
        return;
    }
    if (password === "") {
        $MB.n_warning("密码不能为空！");
        return;
    }
    if (cpassword === "") {
        $MB.n_warning("请再次输入密码！");
        return;
    }
    if (cpassword !== password) {
        $MB.n_warning("两次密码输入不一致！");
        return;
    }
    $.ajax({
        type: "post",
        url: ctx + "user/regist",
        data: {
            "username": username,
            "password": password
        },
        dataType: "json",
        success: function (r) {
            if (r.code === 0) {
                $MB.n_success("注册成功，请登录");
                $(".two input[name='username']").val("");
                $(".two input[name='password']").val("");
                $(".two input[name='cpassword']").val("");
                $('.form-toggle').trigger('click');
            } else {
                $MB.n_warning(r.msg);
            }
        }
    });
}